package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spring.boot.dao.InsuranceMapper;
import com.spring.boot.entity.Insurance;
import com.spring.boot.entity.User;
import com.spring.boot.service.InsuranceService;
import com.spring.boot.utils.ServiceException;
import com.spring.boot.vo.QueryRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Daniel ZTQ
 * @Date: 2021/01/01 22:59
 * @Description: 险种信息业务逻辑层 接口实现
 */
@Slf4j
@Service
public class InsuranceServiceImpl extends ServiceImpl<InsuranceMapper, Insurance> implements InsuranceService {

    @Autowired
    private InsuranceMapper insuranceMapper;

    /**
     * 新增险种实现接口
     */
    @Override
    public Map<String, Object> addInsurance(Insurance insurance) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        try {
            //获取险种名称
            String insuranceName1 = insurance.getInsuranceName();
            //获取险种价位
            Double insurancePrice = insurance.getInsurancePrice();
            //获取有效期
            String insurancePrescription = insurance.getInsurancePrescription();
            //获取险种简介
            String insuranceExplain = insurance.getInsuranceExplain();
            //判断险种名称为空
            if (insuranceName1 == null || insuranceName1.equals("")) {
                //日志打印
                log.info("险种姓名不能为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "险种姓名不能为空！");
                //设置status值
                result.put("status", false);
                //返回result
                return result;
            }
            //判断险种价位为空
            if (insurancePrice == null) {
                //日志打印
                log.info("险种价位不能为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "险种价位不能为空！");
                //设置status值
                result.put("status", false);
                //返回result
                return result;
            }
            //判断险种有效期为空
            if (insurancePrescription == null || insurancePrescription.equals("")) {
                //日志打印
                log.info("险种有效期不能为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "险种有效期不能为空！");
                //设置status值
                result.put("status", false);
                //返回result
                return result;
            }
            //判断险种说明为空
            if (insuranceExplain == null || insuranceExplain.equals("")) {
                //日志打印
                log.info("险种简介不能为空！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "险种简介不能为空！");
                //设置status值
                result.put("status", false);
                //返回result
                return result;
            }
            //如果险种所有信息不为空，则可进行添加险种操作
            //获取险种名称
            String insuranceName = insurance.getInsuranceName();
            //调用sql，根据险种名称查询insurance表中是否存在该险种
            List<Insurance> insuranceByInsuranceName = insuranceMapper.getInsuranceByInsuranceName(insuranceName);
            //如果表中存在该数据
            if (insuranceByInsuranceName.size() > 0) {
                //日志打印
                log.info("该险种已存在！");
                //设置状态码
                result.put("code", 300);
                //设置msg值
                result.put("msg", "该险种已存在！");
                //设置status值
                result.put("status", false);
                //返回result
                return result;
            } else {
                //调用insuranceMapper层的insert的sql，新增险种
                insuranceMapper.insert(insurance);
                //日志打印
                log.info("新增险种成功！");
                //设置状态码
                result.put("code", 200);
                //设置msg值
                result.put("msg", "新增险种成功！");
                //设置status值
                result.put("status", true);
            }
        } catch (Exception e) {
            //日志打印
            log.error("调用新增险种接口失败", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "调用新增险种接口失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("调用新增险种接口失败！");
        }
        //返回result
        return result;
    }

    /**
     * 批量删除险种实现接口
     */
    @Override
    public Map<String, Object> deleteBatchIdsInsurance(String insuranceId) {
        //定义map对象
        Map<String, Object> result = new HashMap<>();
        try {
            //以逗号分隔
            String[] split = insuranceId.split(",");
            //创建list集合
            List<String> idList = new ArrayList<>();
            //for循环遍历，以逗号分隔 要删除的险种
            for (String str : split) {
                //将要删除的险种信息 放入list集合里面
                idList.add(str);
            }
            //调用insuranceMapper层的deleteBatchIds的sql，批量删除险种
            insuranceMapper.deleteBatchIds(idList);
            //日志打印
            log.error("删除险种信息成功!");
            //设置状态码
            result.put("code", 200);
            //设置msg值
            result.put("msg", "删除险种信息成功！");
            //设置status值
            result.put("status", true);
        } catch (Exception e) {
            //日志打印“险种删除失败”
            log.error("险种删除失败", e);
            //设置状态码
            result.put("code", 500);
            //设置msg值
            result.put("msg", "删除险种信息失败！");
            //设置status值
            result.put("status", false);
            //抛出异常
            throw new ServiceException("险种删除失败");
        }
        //返回结果数据
        return result;
    }

    /**
     * 修改险种实现接口
     */
    @Override
    public Map<String, Object> updateInsurance(Insurance insurance) {
        {
            //定义map对象
            Map<String, Object> result = new HashMap<>();
            try {
                //获取险种名称
                String insuranceName = insurance.getInsuranceName();
                //调用sql，根据险种名称查询insurance表中是否存在该险种
                List<Insurance> insuranceByInsuranceName = insuranceMapper.getInsuranceByInsuranceName(insuranceName);
                //如果表中存在该数据
                if (insuranceByInsuranceName.size() > 0) {
                    //日志打印
                    log.info("该险种已存在！");
                    //设置状态码
                    result.put("code", 300);
                    //设置msg值
                    result.put("msg", "该险种已存在！");
                    //设置status值
                    result.put("status", false);
                } else {
                    //调用insuranceMapper层的updateById的sql修改险种信息
                    insuranceMapper.updateById(insurance);
                    //日志打印
                    log.error("修改信息成功");
                    //设置状态码
                    result.put("code", 200);
                    //设置msg值
                    result.put("msg", "修改成功！");
                    //设置status值
                    result.put("status", true);
                }
            } catch (Exception e) {
                //日志打印“险种修改失败”
                log.error("调用险种修改信息接口失败", e);
                //设置状态码
                result.put("code", 500);
                //设置msg值
                result.put("msg", "调用险种修改信息接口失败！");
                //设置status值
                result.put("status", false);
                //抛出异常
                throw new ServiceException("调用险种修改信息接口失败");
            }
            //返回num
            return result;
        }
    }

    /**
     * 筛选险种信息实现接口
     */
    @Override
    public Map<String, Object> findInsuranceList(QueryRequest request, Insurance insurance) {
        {
            //定义map对象
            Map<String, Object> result = new HashMap<>();
            try {
                //创建一个insurance对象
                LambdaQueryWrapper<Insurance> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                //如果获取到的险种名称不为空
                if (StringUtils.isNotEmpty(insurance.getInsuranceName())) {
                    //则根据险种名称进行模糊查询
                    lambdaQueryWrapper.like(Insurance::getInsuranceName, insurance.getInsuranceName());
                }
                //根据险种id进行正序排序
                lambdaQueryWrapper.orderByAsc(Insurance::getInsuranceId);
                //创建分页对象
                Page<Insurance> page = new Page<>(request.getPageNum(), request.getPageSize());
                //进行分页处理
                IPage<Insurance> page1 = this.page(page, lambdaQueryWrapper);
                //设置状态码
                result.put("code", 200);
                //设置data值
                result.put("data", page1);
                //设置msg值
                result.put("msg", "查询险种信息成功！");
                //设置status值
                result.put("status", true);
            } catch (Exception e) {
                //打印日志
                log.error("筛选查询险种信息失败:", e);
                //设置状态码
                result.put("code", 500);
                //设置msg值
                result.put("msg", "调用筛选查询险种信息接口失败！");
                //设置status值
                result.put("status", false);
                //抛出异常
                throw new ServiceException("调用筛选查询险种信息接口失败！");
            }
            // 返回结果
            return result;
        }
    }

    /**
     * 查询所有险种信息实现接口
     */
    @Override
    public Map<String, Object> findInsuranceAllList() {
        {
            //定义map对象
            Map<String, Object> result = new HashMap<>();
            try {
                List<Insurance> insuranceAll = insuranceMapper.getInsuranceAll();
                //设置状态码
                result.put("code", 200);
                //设置data值
                result.put("data", insuranceAll);
                //设置msg值
                result.put("msg", "查询险种列表成功！");
                //设置status值
                result.put("status", true);
            } catch (Exception e) {
                //打印日志
                log.error("查询险种列表失败", e);
                //设置状态码
                result.put("code", 500);
                //设置msg值
                result.put("msg", "查询险种列表失败！");
                //设置status值
                result.put("status", false);
                //抛出异常
                throw new ServiceException("查询险种列表失败！");
            }
            // 返回结果
            return result;
        }
    }

}
